package com.ss6621.spring1.Service.impl;

import com.ss6621.spring1.Mapper.UserMapper;
import com.ss6621.spring1.Service.UserService;
import com.ss6621.spring1.pojo.User;
import com.ss6621.spring1.utils.Result;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;

    public UserServiceImpl(UserMapper userMapper, PasswordEncoder passwordEncoder) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User register(User user) {
        // 密码加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        if (user.getRole()== null) user.setRole("ROLE_USER");
        userMapper.insertUser(user);
        return user;
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public Object update(User user) {
        return userMapper.update( user);
    }
}
